<?php

namespace app\admin\service\setting;

use tmcore\base\BaseService;
use app\common\model\setting\Config as ConfigModel;
use tmcore\services\ConfigService;

/**
 * 网站设置服务类
 */
class WebSetService extends BaseService
{

    /**
     * 获取基础信息
     * @return array
     */
    public static function getWebInfo(): array
    {
        return com_get_config( 'base','sys');
    }

    /**
     * 设置基础信息
     * @param array $params
     * @return bool
     */
    public static function setWebInfo(array $params)
    {
        $data = [
            'name' => $params['name']??'', // 网站名称
            'web_logo' => $params['web_logo']??'', // 网站logo
            'web_favicon' => $params['web_favicon']??'', // 网站favicon

            'app_name' => $params['app_name']??'', // H5端名称
            'app_logo' => $params['app_logo']??'', // H5端logo

            'pc_name' => $params['pc_name']??'', // PC端网站标题
            'pc_logo' => $params['pc_logo']??'', // PC端logo
            'pc_favicon' => $params['pc_favicon']??'', // PC端favicon
            'seo_keyword' => $params['seo_keyword']??'', // 网站关键词
            'seo_desc' => $params['seo_desc']??'', //关键词描述
        ];

        $res = ConfigModel::where(['name' => 'base'])->update(['value' => json_encode($data, 320)]);

        (new ConfigService)->setConfig('create', 'sys', 'base');

        return $res !== false;
    }

    /**
     * 获取备案信息
     * @return array
     */
    public static function getWebRecord(): array
    {
        return com_get_config( 'record','sys');
    }

    /**
     * 设置备案信息
     * @param array $params
     * @return bool
     */
    public static function setWebRecord(array $params)
    {
        $data = [
            'icp' => $params['icp']??'', // icp备案
            'icp_url' => $params['icp_url']??'', // icp备案链接
            'pcp' => $params['pcp']??'', // 公安备案
            'pcp_url' => $params['pcp_url']??'', // 公安备案链接
        ];

        $res = ConfigModel::where(['name' => 'record'])->update(['value' => json_encode($data, 320)]);

        (new ConfigService)->setConfig('create', 'sys', 'record');

        return $res !== false;
    }

    /**
     * 获取地图信息
     * @return array
     */
    public static function getMapInfo(): array
    {
        return com_get_config( 'map.amap','sys');
    }

    /**
     * 设置地图信息
     * @param array $params
     * @return bool
     */
    public static function setMapInfo(array $params)
    {
        $data = [
            'amap' => [
                'key' => $params['key'] ?? '',
                'code' => $params['code'] ?? '',
            ]
        ];

        $res = ConfigModel::where(['name' => 'map'])->update(['value' => json_encode($data, 320)]);

        (new ConfigService)->setConfig('create', 'sys', 'map');

        return $res !== false;
    }

    /**
     * 获取邮箱信息
     * @return array
     */
    public static function getMailInfo(): array
    {
        return com_get_config( 'mail','sys');
    }

    /**
     * 设置邮箱信息
     * @param array $params
     * @return bool
     */
    public static function setMailInfo(array $params)
    {
        $data = [
            'host' => $params['host'] ?? '',
            'user_name' => $params['user_name'] ?? '',
            'password' => $params['password'] ?? '',
            'port' => $params['port'] ?? '',
            'secure_type' => $params['secure_type'] ?? '',
            'form_address' => $params['form_address'] ?? '',
            'form_name' => $params['form_name'] ?? '',
        ];

        $res = ConfigModel::where(['name' => 'mail'])->update(['value' => json_encode($data, 320)]);

        (new ConfigService)->setConfig('create', 'sys', 'mail');

        return $res !== false;
    }

    /**
     * 获取上传信息
     * @return array
     */
    public static function getUploadInfo(): array
    {
        return com_get_config( 'upload','sys');
    }

    /**
     * 设置上传信息
     * @param array $params
     * @return bool
     */
    public static function setUploadInfo(array $params)
    {
        $data = [
            'upload_file_size' => $params['upload_file_size'] ?? '',
            'upload_file_ext' => $params['upload_file_ext'] ?? '',
            'upload_image_size' => $params['upload_image_size'] ?? '',
            'upload_image_ext' => $params['upload_image_ext'] ?? '',
        ];

        $res = ConfigModel::where(['name' => 'upload'])->update(['value' => json_encode($data, 320)]);

        (new ConfigService)->setConfig('create', 'sys', 'upload');

        return $res !== false;
    }
}